package com.mijie.homi.admin.web.reports;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mijie.core.Paging;
import com.mijie.homi.admin.service.reports.ProsecuteService;
import com.mijie.homi.admin.service.user.UserService;
import com.mijie.homi.common.domain.reports.Prosecute;
import com.mijie.homi.common.domain.user.User;

@Controller
@RequestMapping("/prosecute")
public class ProsecuteController {

	@Autowired
	private ProsecuteService service;
	@Autowired
	private UserService userService;
	
	@SuppressWarnings("unchecked")
	@RequestMapping("/list")
	public String list(Paging<Prosecute> paging,Model model,HttpServletRequest request){
		paging.setPageSize(10);
		Map<String,Object> searchBundle = (Map<String, Object>) request.getAttribute("searchBundle");
		service.listProsecute(paging, searchBundle);
		List<Prosecute> list = paging.getList();
		if(!list.isEmpty()){
			User user = null;
			for(Prosecute p : list){
				user = userService.getUserById(p.getDefendant());
				p.setDefendantBlockState(user.getBlock());
			}
		}
		model.addAllAttributes(searchBundle);
		return "prosecute/list";
	}
	
	
	@RequestMapping("/ignore")
	public String ignore(int[] ids,int module,int pageNum,HttpServletRequest request){
		service.ignoreProsecute(ids);
		return "redirect:/prosecute/list.shtml?_module="+module+"&pageNum="+pageNum;
	}
	
	@RequestMapping("/delete")
	public String delete(int[] ids,int module,int pageNum,HttpServletRequest request){
		service.deleteProsecute(ids);
		return "redirect:/prosecute/list.shtml?_module="+module+"&pageNum="+pageNum;
	}
	
	@RequestMapping("/handle")
	public String handle(int[] ids,int module,int pageNum,HttpServletRequest request) throws Exception{
		if(ids!=null){
			service.batchHandle(ids);
		}
		return "redirect:/prosecute/list.shtml?_module="+module+"&pageNum="+pageNum;
	}
	
	@RequestMapping("/blockUser")
	public String block(int plaintiff,int defendant,int module,int pageNum) throws Exception{
		userService.block(defendant,plaintiff);
		return "redirect:/prosecute/list.shtml?_module="+module+"&pageNum="+pageNum;
	}
}
